use axum::Json;
use serde_json::{Value, json};
use crate::{reqwest, query, code};
use super::access_token_kf;

// 上传临时素材，有效期 3 天 upload file = 'media'
async fn upload( corpid:&str, secret:&str, param:Value ) -> Result<Json<Value>, Json<Value>>{
    let access_token = access_token_kf::token(corpid, secret).await?;
    let url = format!(
        "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}",
        access_token
    );
    let rs = reqwest::post(&url,param).await?;  // { type: image/voice/video/file}
    if query::i64(&rs, "errcode")?.eq(&0) {
        code::ok()
    }else {
        code::err("上传素材失败失败")
    }
}
// 获取临时素材
async fn get( corpid:&str, secret:&str, param:Value ) -> Result<Json<Value>, Json<Value>>{
    let access_token = access_token_kf::token(corpid, secret).await?;
    let url = format!(
        "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token={}",
        access_token
    );
    let rs = reqwest::post(&url,param).await?;  // { media_id: }
    if query::i64(&rs, "errcode")?.eq(&0) {
        code::ok()
    }else {
        code::err("获取素材失败")
    }
}